package net.simflow.swf.analyzer.bggraph.opt;

import java.awt.Dimension;

import net.simflow.swf.analyzer.bgnetwork.data.BGLink;
import net.simflow.swf.analyzer.bgnetwork.data.BGNode;
import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class BGVisualizationServerBuilder {
	private static BGVisualizationServerBuilder instance = new BGVisualizationServerBuilder();

	private BGVisualizationServerBuilder() {
	}

	public static BGVisualizationServerBuilder getInstance() {
		return instance;
	}

	public VisualizationViewer<BGNode, BGLink> build(Graph<BGNode, BGLink> g,
			int width, int height) {
		if (g == null) {
			return null;
		}
		if (g.getVertices().isEmpty()) {
			return null;
		}
		ISOMLayout<BGNode, BGLink> layout = new ISOMLayout<BGNode, BGLink>(g);
		layout.setSize(new Dimension(width, height));
		VisualizationViewer<BGNode, BGLink> vs = new VisualizationViewer<BGNode, BGLink>(
				layout, new Dimension(width, height));

		DefaultModalGraphMouse<BGNode, BGLink> gm = new DefaultModalGraphMouse<BGNode, BGLink>();
		gm.setMode(ModalGraphMouse.Mode.PICKING);
		vs.setGraphMouse(gm);

		vs.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

		return vs;
	}
}
